package com.tj.jinzao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.activity.ExchangeRecordActivity;
import com.tj.jinzao.activity.GoodsDetailActivity;
import com.tj.jinzao.adapter.GoodsListItemAdapter;
import com.tj.jinzao.base.BaseDataListFragment;
import com.tj.jinzao.base.IBaseView;
import com.tj.jinzao.base.RecyclerViewGridAdapter;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.Goods;
import com.tj.jinzao.presenter.GoodsPresenter;
import com.tj.jinzao.utils.Utils;
import com.tj.jinzao.widgets.EmptyView;

import java.util.List;

/**
 * 商城列表
 * Created by wjy on 2019/1/1.
 */
public class GoodsListFragment extends BaseDataListFragment<List<Goods>, GoodsPresenter, Goods> implements IBaseView<List<Goods>> {

    private TextView mScoreView;

    public static GoodsListFragment newInstance() {
        GoodsListFragment fragment = new GoodsListFragment();
        if (fragment != null) {
            Bundle args = new Bundle(3);
            args.putString(ARG_TAG_ID, String.valueOf(1));
            args.putInt(ARG_LOAD_ID, 1);
            args.putInt(ARG_LAYOUT_RES_ID, R.layout.base_fragment_item_layout);
            fragment.setArguments(args);
        }
        return fragment;
    }

    @Override
    public void onRequestNoData(String message) {
        setEmptyDataView();
    }

    @Override
    protected GoodsPresenter createPresenter() {
        return new GoodsPresenter(this);
    }

    @Override
    protected void createView(LayoutInflater inflater, View view) {
        View headView = inflater.inflate(R.layout.head_goods, null);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        headView.setLayoutParams(params);
        headView.findViewById(R.id.tv_record).setOnClickListener(this);
        mScoreView = headView.findViewById(R.id.tv_score);
        mScoreView.setText("积分 " + UserManager.getInstance().getUser().getScore());
        mListView.addHeaderView(headView);
    }

    @Override
    protected RecyclerView.Adapter createAdapter() {
        int padding = Utils.dpToPx(12, getResources());
        int left = Utils.dpToPx(20, getResources());
        int itemWidth = (Constant.SCREEN_WIDTH - padding - left * 2) / 2;
        int itemHeight = itemWidth * 205 / 160;
        RecyclerViewGridAdapter adapter = new RecyclerViewGridAdapter(mContext, 2, new GoodsListItemAdapter(mContext, mData, itemWidth));
        adapter.setItemSize(itemWidth, itemHeight);
        adapter.setMargin(left, left, 0, padding, padding);
        return adapter;
    }

    @Override
    protected void requestData() {
        mPresenter.getGoodsList();
    }

    @Override
    protected void setEmptyDataView() {
        mEmptyView.setVisibility(View.VISIBLE);
        mEmptyView.setState(EmptyView.State.NO_DATA, "没有内容哦");
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        startActivity(new Intent(mContext, ExchangeRecordActivity.class));
    }

    @Override
    protected boolean resolveData(List<Goods> lives) {
        return false;
    }

    @Override
    public void onRequestSuccess(List<Goods> goods) {
        super.onRequestSuccess(goods);
        if (mPageNo <= 1) {
            mData.clear();
        }
        mData.addAll(goods);
        showListView();
    }

    @Override
    protected boolean canLoadMoreItems() {
        return false;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void itemClicked(int position, Goods data) {
        startActivity(new Intent(mContext, GoodsDetailActivity.class).putExtra("goods", data));
    }


}
